const treeData = require("./treeData");
const { clone } = require("../cloneDeep");

/**
 *
 *  @description 通过栈来实现
 *  @description 实现：通过维护一个栈，不断的从栈中取出元素，把取出的元素的子节点放入栈中，放到栈的最后面
 * @param { any [] } treeData
 */
function breadthLoop(treeData) {
  const pending = clone(treeData);
  const result = [];
  while (pending.length > 0) {
    const node = pending.shift();
    if (node.children.length > 0) {
      pending.push(...node.children);
    }
    result.push(node.name);
    console.log(node);
  }
  return result;
}
const nodes = breadthLoop(treeData);
console.log(nodes);
